<template>
  <basic-container>
    <el-tabs v-model="activeName" style="margin: 0 10px" @tab-click="handleTabClick">
      <el-tab-pane label="外部序列号" name="serialNumber"></el-tab-pane>
      <el-tab-pane label="外部包装条码" name="packagingBarcode"></el-tab-pane>
    </el-tabs>
    <avue-crud
      v-model="form"
      ref="crud"
      :key="key"
      :option="option"
      :table-loading="loading"
      :defaults.sync="defaults"
      :data="data"
      :page.sync="page"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="success" size="small" icon="el-icon-upload2" @click="handleImport">导入</el-button>

        <!--        <el-upload-->
        <!--          v-if="permission.outsourceProductSerialNumManage_upload"-->
        <!--          ref="uploadOption"-->
        <!--          accept=".xlsx,.xls"-->
        <!--          :limit="1"-->
        <!--          :http-request="uploadFile"-->
        <!--          :file-list="fileList"-->
        <!--          :show-file-list="false"-->
        <!--        >-->
        <!--          <el-button type="primary" size="small" icon="el-icon-upload2">Excel导入</el-button>-->
        <!--        </el-upload>-->
      </template>
      <!--      <template slot="menuRight">-->
      <!--        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>-->
      <!--      </template>-->
    </avue-crud>
    <importDialogComp
      :title="importConfig.title"
      :url="importUrl"
      another-export-method
      :export-name="importConfig.exportName"
      :export-code="importConfig.exportCode"
      :visible.sync="importConfig.show"
      @submit="codeClose"
    ></importDialogComp>
  </basic-container>
</template>

<script>
import importDialogComp from '@/components/dialog/import/index.vue';
import { importOutsourcingByExcel, getOutList, getOutPackingList } from '@/api/product/productSerialNumManage';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';
export default {
  components: {
    importDialogComp,
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      pageCode: 'OUTSOURCE_PRODUCT_SERIAL_NUMBER',
      activeName: 'serialNumber',
      importUrl: '',
      disabled: false,
      key: 'initkey',
      addData: {},
      form: {},
      query: {},
      defaults: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      loading: true,
      fileList: [],
      data: [],
      commonDialog: {
        dispatch: true
      },
      tableOption: {}, //公用模态框配置信息
      selectionList: [],
      importConfig: {
        show: false,
        exportName: '外部产品序列号导入模板.xlsx',
        title: '外部序列号导入',
        exportCode: 'OUTER_SN_IMPORT_TEMPLATE'
      },
      option: {
        height: 'calc(100vh - 490px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 5,
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        cellBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        selection: false,
        dialogClickModal: false,
        menu: false,
        menuWidth: 120,
        searchLabelWidth: 50,
        exportUrl: '/api/mes-product/product/import-excel',
        columnBtn: false,
        column: [
          {
            label: '外部产品序列号',
            prop: 'outerSn',
            search: true,
            searchLabelWidth: 115
          },
          {
            label: '产品编码',
            prop: 'partNo',
            search: true,
            searchLabelWidth: 115
          }
        ]
      }
    };
  },
  created() {},
  computed: {
    ...mapGetters(['permission'])
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    codeClose(value) {
      if (value) {
        this.onLoad(this.page);
      }
    },
    uploadFile(params) {
      const file = params.file;
      let fileParams = new FormData();
      fileParams.append('file', file);
      importOutsourcingByExcel(this.exportUrl, fileParams).then((res) => {
        this.$message({
          type: 'success',
          message: '导入成功!'
        });
        this.onLoad(this.page, this.query);
        this.$refs.uploadOption.clearFiles();
      });

      // this.$refs.uploadOption.clearFiles();
    },

    beforeOpen(done, type) {
      done();
    },
    beforeClose(done, type) {
      done();
    },
    handleImport() {
      this.importUrl = this.activeName === 'serialNumber' ? '/api/mes-product/product/import/outer/sn' : '/api/mes-warehouse/packing/import/outer/sn';
      this.importConfig.show = true;
    },
    handleTabClick({ name }) {
      let column = [];
      let title;
      let exportName;
      let code;
      if (name === 'serialNumber') {
        column = [
          {
            label: '外部产品序列号',
            prop: 'outerSn',
            search: true,
            searchLabelWidth: 115
          },
          {
            label: '产品编码',
            prop: 'partNo',
            search: true,
            searchLabelWidth: 115
          }
        ];
        title = '外部序列号导入';
        exportName = '外部产品序列号导入模板.xlsx';
        code = 'OUTER_SN_IMPORT_TEMPLATE';
      } else {
        column = [
          {
            label: '工单号',
            prop: 'workOrderNo',
            search: true,
            searchLabelWidth: 115
          },
          {
            label: '外部包装条码',
            prop: 'outerPackingSn',
            search: true,
            searchLabelWidth: 115
          },
          {
            label: '包规',
            prop: 'packQuantitySpec'
          }
        ];
        code = 'OUTER_PACKING_SN_IMPORT_TEMPLATE';
        title = '外部包装条码导入';
        exportName = '外部包装条码导入模板.xlsx';
      }
      this.$set(this.option, 'column', column);
      this.importConfig.title = title;
      this.importConfig.exportCode = code;
      this.importConfig.exportName = exportName;
      this.key = Math.random().toString(16).substr(2, 8);
      this.$nextTick(() => {
        this.$structuralSplit();
      });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    async onLoad(page, params = {}) {
      this.loading = true;
      try {
        const getList = this.activeName === 'serialNumber' ? getOutList : getOutPackingList;
        const res = await getList(page.currentPage, page.pageSize, Object.assign(params, this.query));
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      } catch (e) {
        console.warn(e);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep input[type='file'] {
  display: none;
}
</style>
